<%-- This JSP has the HTML for location page. --%>
<%@page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8" %>
<%@ page language="java"%>
<%@ page import="java.util.ResourceBundle" %>
<%@ include file="/WEB-INF/pages/components/docType.jsp" %>
<%
    ResourceBundle bundle = ResourceBundle.getBundle("Text");
%>
<title><%=bundle.getString("changeLocationLabel")%></title>
<style>
input:disabled {background:#dddddd;}
</style>
<script type="text/javascript">//<![CDATA[

function setFieldsFromLocalStorage() {
  document.getElementById("latitude").value=localStorage.getItem("latitude");
  document.getElementById("longitude").value=localStorage.getItem("longitude");
  document.getElementById("accuracy").value=localStorage.getItem("accuracy");

  var useGeoLocation=localStorage.getItem("useGeoLocation");
  if (useGeoLocation==null || useGeoLocation=="true") {
    disableInputs(true);
    document.getElementById("useGeoLocation").checked="checked";
  } else {
    document.getElementById("useOverride").checked="checked";
  }
}

function setFieldsIntoLocalStorage() {
  if (document.getElementById("useGeoLocation").checked) {
    localStorage.setItem("useGeoLocation","true");
  } else {
    localStorage.setItem("useGeoLocation","false");

    // Latitude
    var latitude=document.getElementById("latitude").value;
    localStorage.setItem("latitude",checkFloat(latitude));

    // Longitude
    var longitude=document.getElementById("longitude").value;
    localStorage.setItem("longitude",checkFloat(longitude));

    // Accuracy
    var accuracy=document.getElementById("accuracy").value;
    localStorage.setItem("accuracy",checkFloat(accuracy));
  }
}

// If can be parsed, return float.  Else, return 0.
function checkFloat(value) {
  var returnValue=parseFloat(value);
  if (isNaN(returnValue)){
    returnValue=0;
  }
  return returnValue;
}

function disableInputs(disabled) {
  document.getElementById("latitude").disabled=disabled;
  document.getElementById("longitude").disabled=disabled;
  document.getElementById("accuracy").disabled=disabled;
}

//]]></script>
</head>
<body onload="setFieldsFromLocalStorage()">
<p>
  <input type="radio" name="location" id="useGeoLocation" value="useGeoLocation" onclick="disableInputs(true);"/><label for="useGeoLocation"><%=bundle.getString("currentLocationLabel")%></label>
  <input type="radio" name="location" id="useOverride" value="useOverride" onclick="disableInputs(false);"/><label for="useOverride"><%=bundle.getString("locationBelowLabel")%></label>
</p>
<table>
  <tr><td><%=bundle.getString("latitudeLabel")%>:</td><td><input type="text" name="latitude" id="latitude" title="<%=bundle.getString("latitudeLabel")%>"/></td></tr>
  <tr><td><%=bundle.getString("longitudeLabel")%>:</td><td><input type="text" name="longitude" id="longitude" title="<%=bundle.getString("longitudeLabel")%>"/></td></tr>
  <tr><td><%=bundle.getString("accuracyLabel")%> (m):</td><td><input type="text" name="accuracy" id="accuracy" title="<%=bundle.getString("accuracyLabel")%>"/></td></tr>
</table>
<div style="margin-top:30px">
<%-- Cancel --%>
<input type="submit" name="action" value="<%=bundle.getString("cancelLabel")%>" onclick="window.location='geoNotes.jsp';"/>
<%-- Update --%>
<input type="submit" name="action" style="margin-left:30px" onclick="setFieldsIntoLocalStorage();window.location='geoNotes.jsp'" value="<%=bundle.getString("updateLabel")%>"/>
</div>
</body>
</html>